//
//  YVSTableViewCell.m
//  YoutubeVideoSearchDemo
//
//  Created by eggswift on 2021/9/26.
//

#import "YVSTableViewCell.h"
#import <YYKit/YYKit.h>
#import <SDWebImage/SDWebImage.h>

#define YVS_CHECK_VALID_OBJECT(__aClass, __aObject) ([(id)__aObject isKindOfClass:[__aClass class]])
#define YVS_VALID_OBJECT(__aClass, __aObject) ((__aClass *)(YVS_CHECK_VALID_OBJECT(__aClass, __aObject) ? __aObject : nil))

#define YVS_CHECK_VALID_STRING(__aString) (__aString && [(id)__aString isKindOfClass:[NSString class]] && [(NSString *)__aString length])
#define YVS_VALID_STRING(__aString) (YVS_CHECK_VALID_STRING(__aString) ? (NSString *)__aString : @"")
#define YVS_STRING(__aString, __defaultString) (YVS_CHECK_VALID_STRING(__aString) ? (NSString *)__aString : (NSString *)__defaultString)

#define YVS_CHECK_VALID_NUMBER(__aNumber) (__aNumber && [(id)__aNumber isKindOfClass:[NSNumber class]])
#define YVS_VALID_NUMBER(__aNumber) (YVS_CHECK_VALID_NUMBER(__aNumber) ? (NSNumber *)__aNumber : @(0))
#define YVS_NUMBER(__aNumber, __defaultNumber) (YVS_CHECK_VALID_NUMBER(__aNumber) ? (NSNumber *)__aNumber : (NSNumber *)__defaultNumber)

#define YVS_CHECK_VALID_ARRAY(__aArray) (__aArray && [(id)__aArray isKindOfClass:[NSArray class]] && [(NSArray *)__aArray count])
#define YVS_VALID_ARRAY(__aArray) (YVS_CHECK_VALID_ARRAY(__aArray) ? (NSArray *)__aArray : @[])
#define YVS_ARRAY(__aArray, __defaultArray) (YVS_CHECK_VALID_ARRAY(__aArray) ? (NSArray *)__aArray : (NSArray *)__defaultArray)

#define YVS_CHECK_VALID_DICTIONARY(__aDictionary) (__aDictionary && [(id)__aDictionary isKindOfClass:[NSDictionary class]] && [(NSDictionary *)__aDictionary count])
#define YVS_VALID_DICTIONARY(__aDictionary) (YVS_CHECK_VALID_DICTIONARY(__aDictionary) ? (NSDictionary *)__aDictionary : @{})
#define YVS_DICTIONARY(__aDictionary, __defaultDictionary) (YVS_CHECK_VALID_DICTIONARY(__aDictionary) ? (NSDictionary *)__aDictionary : (NSDictionary *)__defaultDictionary)

@interface YVSTableViewCell ()
@property (nonatomic, strong) UIImageView *thumbImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation YVSTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.thumbImageView];
        [self.contentView addSubview:self.titleLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.thumbImageView.left = 24.0;
    self.thumbImageView.centerY = self.contentView.height / 2.0;
    self.thumbImageView.width = 100.0;
    self.thumbImageView.height = 80.0;

    self.titleLabel.left = self.thumbImageView.right + 12.0;
    self.titleLabel.top = self.thumbImageView.top;
    self.titleLabel.height = self.thumbImageView.height;
    self.titleLabel.width = self.contentView.width - self.thumbImageView.right - 8.0 - 16.0;
}

- (void)setData:(NSDictionary *)data {
    if (![data isKindOfClass:[NSDictionary class]]) {
        return;
    }

    _data = data;

    NSDictionary *compactVideoRenderer = YVS_VALID_DICTIONARY(_data[@"compactVideoRenderer"]);
    
    NSString *urlString = YVS_VALID_STRING(YVS_VALID_DICTIONARY(YVS_VALID_ARRAY(YVS_VALID_DICTIONARY(compactVideoRenderer[@"thumbnail"])[@"thumbnails"]).firstObject)[@"url"]);
    [self.thumbImageView sd_setImageWithURL:[NSURL URLWithString:urlString]];
    
    NSString *title = YVS_VALID_STRING(YVS_VALID_DICTIONARY(YVS_VALID_ARRAY(YVS_VALID_DICTIONARY(compactVideoRenderer[@"title"])[@"runs"]).firstObject)[@"text"]);
    [self.titleLabel setText:YVS_CHECK_VALID_STRING(title) ? title : @"Unknown"];
}

#pragma mark - SET/GET
- (UIImageView *)thumbImageView {
    if (!_thumbImageView) {
        _thumbImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _thumbImageView.backgroundColor = [UIColor lightGrayColor];
        _thumbImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _thumbImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = [UIColor darkGrayColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:15.0];
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

@end
